﻿using MovingChame.WindowsStoreApp.Helpers;
using MovingChameLibrary.EnumTypes;
using MovingChameLibrary.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MovingChame.WindowsStoreApp.ViewModels
{
    public class AdvancedSearchPageViewModel : AdvancedSearchViewModel
    {
        private string _button_SortByTitle_Content;
        public string Button_SortByTitle_Content
        {
            get { return this._button_SortByTitle_Content; }
            set { this.SetProperty(ref this._button_SortByTitle_Content, value, "Button_SortByTitle_Content"); }
        }

        private string _button_SortByBudget_Content;
        public string Button_SortByBudget_Content
        {
            get { return this._button_SortByBudget_Content; }
            set { this.SetProperty(ref this._button_SortByBudget_Content, value, "Button_SortByBudget_Content"); }
        }

        private bool _searchProgressBar_IsIndeterminate = false;
        public bool SearchProgressBar_IsIndeterminate
        {
            get { return this._searchProgressBar_IsIndeterminate; }
            set { this.SetProperty(ref this._searchProgressBar_IsIndeterminate, value, "SearchProgressBar_IsIndeterminate"); }
        }

        private bool _resultHeader_Visibility = false;
        public bool ResultHeader_Visibility
        {
            get { return this._resultHeader_Visibility; }
            set { this.SetProperty(ref this._resultHeader_Visibility, value, "ResultHeader_Visibility"); }
        }

        public override ICommand SortByTitleCommand
        {
            get
            {
                if (_sortByTitleCommand == null)
                {
                    _sortByTitleCommand = new RelayCommand(new Action(SortByTitleExecuted), new Func<bool>(SortByTitleCanExecute), false);
                }
                return _sortByTitleCommand;
            }
        }

        public bool SortByTitleCanExecute()
        {
            return true;
        }

        public void SortByTitleExecuted()
        {
            resetSortButtonContent();
            if (SortingCriteria != SortCriteria.Title)
            {
                sortByTitleAscending();
                Button_SortByTitle_Content = LabelContentHelper.GetSortLabelContent(Button_SortByTitle_Content, SortDirection.Ascending);
            }
            else
            {
                if (SortingDirection == SortDirection.Ascending)
                {
                    sortByTitleDescending();
                    Button_SortByTitle_Content = LabelContentHelper.GetSortLabelContent(Button_SortByTitle_Content, SortDirection.Descending);
                }
                else
                {
                    sortByTitleAscending();
                    Button_SortByTitle_Content = LabelContentHelper.GetSortLabelContent(Button_SortByTitle_Content, SortDirection.Ascending);
                }
            }
        }

        public override ICommand SortByBudgetCommand
        {
            get
            {
                if (_sortByBudgetCommand == null)
                {
                    _sortByBudgetCommand = new RelayCommand(new Action(SortByBudgetExecuted), new Func<bool>(SortByBudgetCanExecute), false);
                }
                return _sortByBudgetCommand;
            }
        }

        public bool SortByBudgetCanExecute()
        {
            return true;
        }

        public void SortByBudgetExecuted()
        {
            resetSortButtonContent();
            if (SortingCriteria != SortCriteria.Budget)
            {
                sortByBudgetAscending();
                Button_SortByBudget_Content = LabelContentHelper.GetSortLabelContent(Button_SortByBudget_Content, SortDirection.Ascending);
            }
            else
            {
                if (SortingDirection == SortDirection.Ascending)
                {
                    sortByBudgetDescending();
                    Button_SortByBudget_Content = LabelContentHelper.GetSortLabelContent(Button_SortByBudget_Content, SortDirection.Descending);
                }
                else
                {
                    sortByBudgetAscending();
                    Button_SortByBudget_Content = LabelContentHelper.GetSortLabelContent(Button_SortByBudget_Content, SortDirection.Ascending);
                }
            }
        }

        private void resetSortButtonContent()
        {
            Button_SortByTitle_Content = "Title";
            Button_SortByBudget_Content = "Budget";
        }

        public override ICommand SearchCommand
        {
            get
            {
                if (_searchCommand == null)
                {
                    _searchCommand = new RelayCommand(new Action(SearchExecuted), new Func<bool>(SearchCanExecute), false);
                }
                return _searchCommand;
            }
        }

        public bool SearchCanExecute()
        {
            return true;
        }

        public void SearchExecuted()
        {
            SearchProgressBar_IsIndeterminate = true;
            resetSortButtonContent();
            resetSort();
            setSearchData();
            Items.Sort(o => o.Title, SortDirection.Ascending);
            Button_SortByTitle_Content = LabelContentHelper.GetSortLabelContent(Button_SortByTitle_Content, SortDirection.Ascending);
            ResultHeader_Visibility = true;
        }
    }
}